﻿using System;
using System.Linq;
using System.ServiceModel.Syndication;
using Api.Model;
using Articles = System.Collections.Generic.IEnumerable<Api.Model.Article>;

namespace RssProxyService
{
    public abstract class AbstractRssMapper : IRssMapper
    {
        public Articles GetMappedItems(string rssString)
        {
            try
            {
                SyndicationFeed feed = rssString.GetSyndicationFeed();
                return GetMappedItems(feed);
            }
            catch (Exception e)
            {
                return Enumerable.Empty<Article>();
            }
        }

        public Articles GetMappedItems(SyndicationFeed feed)
        {
            try
            {
                var articles = feed.Items.Select(syndicationItem =>
                {
                    return MapSingleArticle(syndicationItem);
                });

                return articles;
            }
            catch (Exception e)
            {
                return Enumerable.Empty<Article>();
            }
        }

        public abstract Article MapSingleArticle(SyndicationItem syndicationItem);
    }
}
